package org.han.org.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.han.annotation.RonghuanetPermission;
import org.han.org.domain.Employee;
import org.han.org.query.EmployeeQuery;
import org.han.org.service.IEmployeeService;
import org.han.util.AjaxResult;
import org.han.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("/employee")
@Api(value = "员工的API",description="员工相关的CRUD功能")
@RonghuanetPermission(name="员工管理",desc = "员工的增删改查")
public class EmployeeController {
    @Autowired
    private IEmployeeService employeeService;

    @GetMapping
    @ApiOperation(value = "查询所有" )
    @RonghuanetPermission(name="员工的查询所有")
    public AjaxResult selectAll(){
        try {
            List<Employee> employees = employeeService.selectAll();
            return AjaxResult.me().setData(employees);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("查询失败~"+ e.getMessage());
        }
    }
    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查询" )
    @RonghuanetPermission(name="员工的根据id查询")
    public AjaxResult selectById(@PathVariable("id")Long id){
        try {
            Employee employee = employeeService.selectById(id);
            return AjaxResult.me().setData(employee);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("查询失败~"+ e.getMessage());
        }
    }
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除")
    @RonghuanetPermission(name="员工的删除")
    public AjaxResult delete(@PathVariable("id")Long id){
        try {
            employeeService.delete(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("查询失败~"+ e.getMessage());
        }
    }
    @PutMapping
    @ApiOperation(value = "新增/修改" )
    @RonghuanetPermission(name="员工的新增/修改")
    public AjaxResult addorudpate(@RequestBody Employee employee){
        try {
            if(Objects.isNull(employee.getId())){
                // 新增
                employeeService.insert(employee);
                return AjaxResult.me();
            }else {
                // 修改
                employeeService.update(employee);
                return AjaxResult.me();
            }
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("保存失败~" + e.getMessage());
        }
    }

    @PostMapping
    @ApiOperation(value = "分页查询")
    @RonghuanetPermission(name="员工的分页查询")
    public AjaxResult pageList(@RequestBody EmployeeQuery query){
        try {
            PageList employee = employeeService.pageList(query);
            return AjaxResult.me().setData(employee);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("查询失败~" + e.getMessage());
        }
    }
    @PatchMapping
    @ApiOperation(value = "批量删除")
    @RonghuanetPermission(name="员工的批量删除")
    public AjaxResult batchDelete(@RequestBody List<Long> ids){
        try {
            employeeService.batchDelete(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("删除失败~" + e.getMessage());
        }
    }
}
